const DB = {
  objectStore: [
    { storeName: 'projectGRDraft', keyPath: 'id', index: ['userID'] }, // 等级修理项目草稿
    { storeName: 'projectInsDraft', keyPath: 'id', index: ['userID'] }, // 巡检巡修项目草稿
    { storeName: 'projectFinDraft', keyPath: 'id', index: ['userID'] }, // 器材筹供项目草稿
    { storeName: 'projectTsDraft', keyPath: 'id', index: ['userID'] }, // 器材筹供项目草稿
  ],
  openDB: function (dbName, storeName, version = new Date().getTime()) {
    return new Promise((resolve, reject) => {
      var indexedDB =
        window.indexedDB ||
        window.mozIndexedDB ||
        window.webkitIndexedDB ||
        window.msIndexedDB;
      let db;
      const request = indexedDB.open(dbName, version);
      request.onsuccess = (event) => {
        // console.log('数据库打开成功！', event);
        db = event.target.result;
        resolve(db);
      };

      request.onerror = (event) => {
        // console.error('数据库打开失败！');
        reject(event);
      };

      request.onupgradeneeded = (event) => {
        // console.log('数据库创建成功');
        db = event.target.result;
        var objectStore;
        DB.objectStore.forEach(item => {
          if (!db.objectStoreNames.contains(item.storeName)) {
            objectStore = db.createObjectStore(item.storeName, { keyPath: item.keyPath });
            item.index.forEach(index => {
              objectStore.createIndex(index, index, { unique: false });
            });
          };
        });
      };
    });
  },
  getAll: function (db, storeName) {
    return new Promise((resolve, reject) => {
      var transaction = db.transaction([storeName]);
      var objectStore = transaction.objectStore(storeName);
      var request = objectStore.getAll();

      request.onerror = (event) => {
        reject(event);
      };

      request.onsuccess = (event) => {
        resolve(request.result);
      };
    });
  },
  addData: function(db, storeName, data) {
    return new Promise((resolve, reject) => {
      var request = db
        .transaction([storeName], 'readwrite')
        .objectStore(storeName)
        .add(data);

      request.onsuccess = (event) => {
        // console.log('数据写入成功');
        resolve(event);
      };

      request.onerror = (event) => {
        // console.log('数据写入失败');
        reject(event);
      };
    });
  },
  getDataByKey: function(db, storeName, key) {
    return new Promise((resolve, reject) => {
      var transaction = db.transaction([storeName]);
      var objectStore = transaction.objectStore(storeName);
      var request = objectStore.get(key);

      request.onerror = (event) => {
        reject(event);
      };

      request.onsuccess = (event) => {
        resolve(request.result);
      };
    });
  },
  cursorGetData: function(db, storeName) {
    return new Promise((resolve, reject) => {
      var list = [];
      var store = db
        .transaction(storeName, 'readwrite')
        .objectStore(storeName);
      var request = store.openCursor();
      request.onsuccess = (e) => {
        var cursor = e.target.result;
        if (cursor) {
          list.push(cursor.value);
          cursor.continue();
        } else {
          // console.log('游标查询结果：', list);
        };
        resolve(e);
      };

      request.onerror = (e) => {
        reject(e);
      };
    });
  },
  getDataByIndex: function(db, storeName, indexName, indexValue) {
    return new Promise((resolve, reject) => {
      var store = db.transaction(storeName, 'readwrite').objectStore(storeName);
      var request = store.index(indexName).get(indexValue);
      request.onerror = (e) => {
        // console.log('事务失败');
        reject(e);
      };
      request.onsuccess = (e) => {
        var result = e.target.result;
        // console.log('索引查询结果：', result);
        resolve(result);
      };
    });
  },
  cursorGetDataByIndex: function(db, storeName, indexName, indexValue) {
    return new Promise((resolve, reject) => {
      var list = [];
      var store = db.transaction(storeName, 'readwrite').objectStore(storeName);
      var request = store
        .index(indexName)
        .openCursor(IDBKeyRange.only(indexValue));
      request.onsuccess = (e) => {
        var cursor = e.target.result;
        if (cursor) {
          list.push(cursor.value);
          cursor.continue();
        } else {
          // console.log('游标索引查询结果：', list);
          resolve(list);
        }
      };
      request.onerror = (e) => {
        reject(e);
      };
    });
  },
  updateDB: function(db, storeName, data) {
    return new Promise((resolve, reject) => {
      var request = db
        .transaction([storeName], 'readwrite')
        .objectStore(storeName)
        .put(data);

      request.onsuccess = (e) => {
        // console.log('数据更新成功');
        resolve(e);
      };

      request.onerror = (e) => {
        // console.log('数据更新失败');
        reject(e);
      };
    });
  },
  deleteDB: function(db, storeName, id) {
    return new Promise((resolve, reject) => {
      var request = db
        .transaction([storeName], 'readwrite')
        .objectStore(storeName)
        .delete(id);

      request.onsuccess = (e) => {
        // console.log('数据删除成功');
        resolve(e);
      };

      request.onerror = (e) => {
        // console.log('数据删除失败');
        reject(e);
      };
    });
  },
  cursorDelete: function(db, storeName, indexName, indexValue) {
    return new Promise((resolve, reject) => {
      var store = db.transaction(storeName, 'readwrite').objectStore(storeName);
      var request = store
        .index(indexName)
        .openCursor(IDBKeyRange.only(indexValue));
      request.onsuccess = (e) => {
        var cursor = e.target.result;
        var deleteRequest;
        if (cursor) {
          deleteRequest = cursor.delete();
          deleteRequest.onerror = (e) => {
            // console.log('游标删除该记录失败');
            cursor.continue();
            reject(e);
          }
          deleteRequest.onsuccess = (e) => {
            // console.log('游标删除该记录成功');
            cursor.continue();
            resolve(e);
          }
        };
      };
      request.onerror = (e) => {
        reject(e);
      };
    });
  },
  closeDB: function(db) {
    db.close();
  },
  deleteDBAll: function(dbName) {
    return new Promise((resolve, reject) => {
      var deleteRequest = window.indexedDB.deleteDatabase(dbName);
      deleteRequest.onerror = (event) => {
        // console.log('删除失败');
        reject(event);
      };
      deleteRequest.onsuccess = (event) => {
        // console.log('删除成功');
        resolve(event);
      };
    });
  }
};
